package com.easysoft.framework.chat.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.chat.mapper.SysChatroomMapperExtd;
import com.easysoft.framework.chat.model.SysChatroom;
import com.easysoft.framework.chat.model.SysChatroomQuery;
import com.easysoft.framework.chat.model.SysChatroomView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("service.SysChatroomService")
public class SysChatroomService {
    @Autowired
    @Qualifier("mapper.SysChatroomMapperExtd")
    private SysChatroomMapperExtd sysChatroomMapperExtd;

    @DataSource(DataSource.MASTER)
    public int addSysChatroom(SysChatroom entity){
        entity.setId(UUIDUtils.getUUID());
        entity.setCreateTime(new Date());
        return sysChatroomMapperExtd.insertSelective(entity);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysChatroom(String id){
        return sysChatroomMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysChatroom(SysChatroom entity){
        return sysChatroomMapperExtd.updateByPrimaryKeySelective(entity);
    }

    @DataSource(DataSource.MASTER)
    public SysChatroom selectSysChatroomById(String id){
        return sysChatroomMapperExtd.selectByPrimaryKey(id);
    }


    @DataSource(DataSource.SLAVE)
    public List<SysChatroomView> selectSysChatroomListByPage(SysChatroomQuery query, Page page){
        int maxRow = sysChatroomMapperExtd.selectCountSysChatroom(query);
        page.setMaxRow(maxRow);

        if (maxRow == 0) {
            return new ArrayList<SysChatroomView>();
        }
        return sysChatroomMapperExtd.selectSysChatroomListByPage(query,page);
    }
}
